﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ScrumManagementTool.domain;
using ScrumManagementTool.domain.interfaces;
using ScrumManagementTool.service;

namespace ScrumManagementToolUserControls
{
    /// <summary>
    /// Interaction logic for OpenProjectUserControl.xaml
    /// </summary>
    public partial class OpenProjectUserControl : UserControl
    {

        private MainWindow parentWindow;
        private ProjectService projectService = new ProjectService();

        public OpenProjectUserControl()
        {
            InitializeComponent();
            ProjectService projectService = new ProjectService();
            lstProjectNames.ItemsSource = projectService.GetAllProjectNames();
        }

        private void btnCancelOpenProject_Click(object sender, RoutedEventArgs e)
        {
            Visibility = Visibility.Collapsed;
        }

        private void btnOpenProject_Click(object sender, RoutedEventArgs e)
        {
            var selectedProjects = lstProjectNames.SelectedItems;
            IList<IProject> allProjects = new List<IProject>();

            foreach (var projectName in selectedProjects)
            {
                IProject project = new Project { Name = projectName.ToString() };
                allProjects.Add(project);
            }

//            IProject project = projectService.CreateNewProject(txtProjectName.Text);
            parentWindow = GetTopParent() as MainWindow;
            parentWindow.projectTree.BuildTree(allProjects);
            Visibility = Visibility.Collapsed;
        }

        private Window GetTopParent()
        {
            DependencyObject dpParent = Parent;
            do
            {
                dpParent = LogicalTreeHelper.GetParent(dpParent);
            } while (dpParent.GetType().BaseType != typeof(Window));
            return dpParent as Window;
        }
    }
}
